package cn.jbt.api.admin.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.*;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EntityListeners(AuditingEntityListener.class)
@Entity
@Table(name = "sys_menu", schema = "jbt")
public class SysMenu {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @NotBlank(message = "菜单名称不能为空")
    @Column(name = "menu_name", nullable = false, length = 50)
    private String menuName;

    @NotNull(message = "排序属性不能为空")
    @Min(value = 0, message = "排序属性'值'错误")
    @Max(value = 1000000000, message = "排序属性'值'错误")
    @Column(name = "orderby", nullable = false)
    private Integer orderby;

    @NotNull(message = "当前状态不能为空")
    @Min(value = 0, message = "当前状态'值'错误")
    @Max(value = 1, message = "当前状态'值'错误")
    @Column(name = "status", nullable = false)
    private Integer status;

    @Column(name = "node", nullable = false)
    private String node = "";

    @Column(name = "floor", nullable = false)
    private Integer floor = 1;

    @Column(name = "path", nullable = false, length = 200)
    private String path = "";

    @Column(name = "component", nullable = false)
    private String component = "";

    @Column(name = "query", nullable = false)
    private String query = "";

    @NotNull(message = "是否外链不能为空")
    @Min(value = 0, message = "是否外链'值'错误")
    @Max(value = 1, message = "是否外链'值'错误")
    @Column(name = "is_frame", nullable = false)
    private Integer isFrame;

    @Column(name = "menu_type", nullable = false, length = 1)
    private String menuType = "";

    @NotNull(message = "显示状态不能为空")
    @Min(value = 0, message = "显示状态'值'错误")
    @Max(value = 1, message = "显示状态'值'错误")
    @Column(name = "visible", nullable = false)
    private Integer visible;

    @Column(name = "permission", nullable = false, length = 100)
    private String permission = "";

    @Column(name = "icon", nullable = false, length = 100)
    private String icon = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @CreatedDate
    @Column(name = "create_time", nullable = false)
    private Date createTime;

    @Column(name = "create_by", nullable = false, length = 64)
    private String createBy = "";

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private Date updateTime;

    @Column(name = "update_by", nullable = false, length = 64)
    private String updateBy = "";

    @Column(name = "remark", nullable = false)
    private String remark = "";

    @Min(value = 0, message = "所属父级'值'错误")
    @Column(name = "pid", nullable = false)
    private Long pid = 0L;

    public void edit(SysMenu sysMenu) {
        this.menuName = sysMenu.menuName;
        this.orderby = sysMenu.orderby;
        this.status = sysMenu.status;
        this.path = sysMenu.path;
        this.component = sysMenu.component;
        this.query = sysMenu.query;
        this.isFrame = sysMenu.isFrame;
        this.menuType = sysMenu.menuType;
        this.visible = sysMenu.visible;
        this.permission = sysMenu.permission;
        this.icon = sysMenu.icon;
        this.remark = sysMenu.remark;
    }

}
